package pre.liuxl.blog.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.bytebuddy.asm.Advice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import pre.liuxl.blog.entity.User;
import pre.liuxl.blog.entity.UserArticle;
import pre.liuxl.blog.service.UserArticleService;
import pre.liuxl.blog.utils.QiniuyunUtil;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by liuxuanlong on 2018/6/24.
 */
@Controller
public class UserArticleController {
    private final static Logger logger = LoggerFactory.getLogger(UserArticle.class);
    @Autowired
    UserArticleService userArticleService;

    @RequestMapping("/editeArticle")
    public String editeArticle(Model model) {


        return "admin/editeArticle";
    }

    /**
     * layui富文本编辑器上传图片
     *
     * @param file
     * @return
     */
    @RequestMapping("/uploadArticleImg")
    @ResponseBody
    public JSONObject uploadArticleImg(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        StringBuilder stringBuilder = new StringBuilder();
        try {
             fileName = UUID.randomUUID().toString()+suffixName;
            QiniuyunUtil.uploadFiles(file.getBytes(), fileName);
            stringBuilder.append("http://patkem0hy.bkt.clouddn.com/").append(fileName);
            JSONObject jsonObject = new JSONObject();
            JSONObject jsonObject1 = new JSONObject();
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传成功");
            jsonObject1.put("src", stringBuilder.toString());
            jsonObject.put("data", jsonObject1);
            return jsonObject;
        } catch (IOException e) {
            JSONObject jsonObject = new JSONObject();
            JSONObject jsonObject1 = new JSONObject();
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败");
            e.printStackTrace();
            return jsonObject;
        }


    }

    /**
     * 添加文章
     *
     * @param httpSession
     * @param headline
     * @param articleClass
     * @param content
     * @return
     */
    @RequestMapping("/addArticle")
    @ResponseBody
    public String addArtile(HttpSession httpSession, String headline, String articleClass, String content) {
        logger.info(headline, articleClass, content);
        User loginUser = (User) httpSession.getAttribute("loginUser");
        Integer loginUserId = loginUser.getId();
        userArticleService.saveUserArticle(loginUserId, headline, articleClass, content);
        return "添加成功";
    }

    @RequestMapping("/article/{id}")
    public String findArticle(@PathVariable Integer id, Model model) {
        model.addAttribute("articleId", id);
        model.addAttribute("article", userArticleService.findById(id));
        model.addAttribute("conmentList", userArticleService.findByArticleId(id));
        return "article";
    }

    @RequestMapping("/addComment")
    @ResponseBody
    public void addComment(Integer articleId, String content, HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        Integer loginUserId = loginUser.getId();
        userArticleService.saveComment(articleId, loginUserId, content);
    }
}
